<template>
  <div>
           <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">

         <el-form-item label="班级">
          <el-input
            v-model="lesson"
            placeholder="请输入班级"
          ></el-input>
        </el-form-item>

        <el-form-item label="学生学号">
          <el-input
            v-model="susername"
            placeholder="请输入学生学号"
          ></el-input>
        </el-form-item>
       
        <el-form-item label="实验名">
          <el-input
            v-model="experiment"
            placeholder="请输入实验名"
          ></el-input>
        </el-form-item>
         <el-form-item label="实验室位置">
          <!-- <el-input
            v-model="local"
            placeholder="请输入实验室位置"
          ></el-input> -->
          <el-select v-model="local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="实验日期">
           <el-date-picker
            v-model="date"
              type="date"
              value-format="yyyy-MM-dd" 
              placeholder="选择日期">
            </el-date-picker>
        </el-form-item>

        <el-form-item label="实验时间">
            <el-select v-model="time" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="info" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>

      <!--按钮-->
      <el-row>
        <el-button type="danger" size="medium" @click="deleteByIds"
          >- 批量删除</el-button
        >
        <el-button type="primary" size="medium" @click="addVisible = true;">+ 新增学生实验报告</el-button>

        <el-upload
              class="upload-demo"
              action="http://localhost:8080/homeworks/insertBatch"
              :show-file-list= "false"
              :on-success="handFileSuccess"
              style="display: inline-block; margin-left : 10px"
            >
              <el-button type="primary" plain >+ 批量导入学生实验报告</el-button>
            </el-upload>
      </el-row>
    </el-card>
    
      <el-dialog title="添加实验报告" :visible.sync="addVisible"  width="40%">
        <el-form :model="user" label-width="80px" style="padding-right : 20px" :rules="rules">
            <el-form-item label="教师号" prop="username">
            <el-input v-model="user.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="班级" prop="lesson">
            <el-input v-model="user.lesson"></el-input>
            </el-form-item>
            <el-form-item label="学生学号" prop="susername">
            <el-input v-model="user.susername"></el-input>
            </el-form-item>
            <el-form-item label="实验名" prop="experiment">
            <el-input v-model="user.experiment"></el-input>
            </el-form-item>
            <el-form-item label="实验描述" prop="description">
            <!-- <el-input v-model="user.description"></el-input> -->
            <el-input type="textarea"
              autosize  maxlength="100"
              show-word-limit v-model="user.description"></el-input>
            </el-form-item>
            <el-form-item label="实验室位置" prop="local">
            <!-- <el-input v-model="user.local"></el-input> -->
              <el-select v-model="user.local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
            </el-form-item>
            <el-form-item label="实验日期" prop="date">
            <el-date-picker
              v-model="user.date"
                type="date"
                value-format="yyyy-MM-dd" 
                placeholder="选择日期">
            </el-date-picker>
            </el-form-item>

            <el-form-item label="实验时间" prop="time">
            <el-select v-model="user.time" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="insert()">提交</el-button>
            <el-button @click="addVisible = false;">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>


     <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
       <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="教师号"> </el-table-column>
      <el-table-column prop="lesson" label="班级"> </el-table-column>
      <el-table-column prop="susername" label="学生学号"> </el-table-column>
      <el-table-column prop="experiment" label="实验名"></el-table-column>
      <el-table-column prop="description" label="实验描述"></el-table-column>
      <el-table-column prop="local" label="实验室位置"> </el-table-column>
      <el-table-column prop="date" label="实验日期"> </el-table-column>
      <el-table-column prop="time" label="时间"> </el-table-column>
      <el-table-column prop="file" label="文件地址"> </el-table-column>
      <el-table-column prop="grade" label="成绩"> </el-table-column>
    

     <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="update(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      </el-table>  

   <el-row>
      <div class="block" style="margin:10px 0">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
    </el-row>

 <el-dialog title="评阅实验报告" :visible.sync="formVisible"  width="40%">
        <el-form :model="form" label-width="80px" style="padding-right : 20px" :rules="rules">
            
           <el-form-item label="班级" prop="lesson">
            <el-input v-model="form.lesson" disabled></el-input>
            </el-form-item>
            <el-form-item label="学生学号" prop="susername">
            <el-input v-model="form.susername" disabled></el-input>
            </el-form-item>
         
            <el-form-item label="实验名" prop="experiment">
            <el-input v-model="form.experiment" disabled></el-input>
            </el-form-item>

            <el-form-item label="学生成绩" prop="grade">
            <el-input-number v-model="form.grade1" :min="0" :max="100"></el-input-number>
            </el-form-item>

            <el-form-item label="实验报告文件">
            <el-input v-model="form.file" disabled></el-input>
            <el-button size="small" type="primary" @click="download(form.file)">点击下载</el-button>        
            </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="empty()">打回报告</el-button>
            <el-button type="primary" @click="save()">提交</el-button>
            <el-button @click="formVisible = false">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>

    
  </div>
</template>

<script>
export default {
  name: 'THomework',
  data () {
    return {
      user: JSON.parse(localStorage.getItem("honey-user") || "{}"),
      lesson :'',
      susername : '',
      experiment : '',
      local : '',
      date : '',
      time : '',
      tableData:[], // 所有数据
      page : 1, // 当前的页码
      pageSize : 5, // 每页显示的个数
      total : 0,
      ids : [],
      addVisible : false,
      formVisible : false,
      form : {},
      labors : [],
      options: [{
          value: '08:20-09:55',
          label: '08:20-09:55'
        }, {
          value: '10:10-11:45',
          label: '10:10-11:45'
        }, {
          value: '10:10-12:30',
          label: '10:10-12:30'
        }, {
          value: '13:20-14:55',
          label: '13:20-14:55'
        }, {
          value: '15:10-16:45',
          label: '15:10-16:45'
        },{
          value: '15:10-17:30',
          label: '15:10-17:30'
        },{
          value: '18:15-19:50',
          label: '18:15-19:50'
        },{
          value: '18:15-20:35',
          label: '18:15-20:35'
        }],
        rules: {
          lesson: [
            { required: true, message: '请输入学生班级', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 15 个字符', trigger: 'blur' }
          
          ],
          susername: [
            { required: true, message: '请输入学生学号', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 15 个字符', trigger: 'blur' }
          
          ],
          
          experiment: [
            { required: true, message: '请输入实验名', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          
          ],
          local : [
            { required: true, message: '请输入实验室位置', trigger: 'blur' }
          ],
          date : [
            { required: true, message: '请输入日期', trigger: 'blur' }
          ],
          time : [
            { required: true, message: '请输入日期', trigger: 'blur' }
          ],
          grade : [
            { required: true, message: '请输入学生成绩', trigger: 'blur' }
          ]

      }


    }
  },
  created () {
    this.load();
    this.loadLabors();
  },
  methods: {
    handFileSuccess(res){
      // console.log(res);
      if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
         this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'error'
          });
          }
    },
    loadLabors(){
            this.$request.get('/labors/selectAll').then((result) => {
                this.labors = result.data;
            }).catch((err) => {
                console.log(err);
            });
        },
    save(){
      this.form.grade = this.form.grade1;
       this.$request.put('/homeworks', this.form).then((res) => {
        if(res.code === 1){
                this.$message({
                //message: '登陆成功',
                message:res.msg,
                type: 'success'
            });
            this.load(1);
            this.formVisible = false;
            }else{
                 this.$message({
                //message: '登陆失败',
                message: res.msg,
                type: 'warning'
            });
            }
      }).catch((err) => {
        console.log(err);
      });

    },
    empty(){
      this.form.file = '';
       this.$request.put('/homeworks', this.form).then((res) => {
        if(res.code === 1){
                this.$message({
                //message: '登陆成功',
                message:res.msg,
                type: 'success'
            });
            this.load(1);
            this.formVisible = false;
            }else{
                 this.$message({
                //message: '登陆失败',
                message: res.msg,
                type: 'warning'
            });
            }
      }).catch((err) => {
        console.log(err);
      });
    },
    download(file){
      location.href = file;
    },
     update(row){ // 编辑
        this.form = JSON.parse(JSON.stringify(row)); // 给student对象赋值
        this.formVisible = true; // 显示弹窗
    },
    insert(){
       this.$request.post("/homeworks",this.user).then((res) => {
        if(res.code === 1){
                this.$message({
                //message: '登陆成功',
                message:res.msg,
                type: 'success'
            });
            this.load(1);
            this.addVisible = false;
            }else{
                 this.$message({
                //message: '登陆失败',
                message: res.msg,
                type: 'warning'
            });
            }
      }).catch((err) => {
        console.log(err);
      });

    },

    handleSelectionChange(rows) {
        this.ids = rows.map(v => v.id);
        console.log(this.ids);
    },
    deleteById(id){
       this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/homeworks/' + id ).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
     deleteByIds(){
      if(!this.ids.length){
        this.$message.warning('请选择数据！');
        return;
      }

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/homeworks/'+ this.ids).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
    load(page){ // 分页查询
        if (page) {
            this.page = page;
        }
        this.$request.get('/homeworks',{
            params : {
                page : this.page,
                pageSize : this.pageSize,
                username: this.user.username,
                lesson: this.lesson,
                susername:this.susername,
                experiment:  this.experiment,
                local : this.local,
                date : this.date,
                time : this.time

            }
        }).then((res) => {
            this.total = res.data.total;
            this.tableData = res.data.rows;
        }).catch((err) => {
            console.log(err);
        });
    },
    handleCurrentChange(page){
      //  this.page = page;
        this.load(page);
    },
    reset(){
        this.lesson = '';
        this.susername = '';
        this.experiment = '';
        this.local = '';
        this.date = '';
        this.time = '',
        this.load(1);
    }

  }
  
  
}
</script>

<style>

</style>